var baseJs = {
	system_onload_bool: false,
	tips: function(msg) {
		if (self != top) {
			top.window.baseJs.tips(msg);
			return;
		}
		var jq = top.$;
		var tipsDiv = jq('<div align="center">' + msg + '</div>');
		jq(top.window.document.body).append(tipsDiv);
		var width = top.window.document.body.clientWidth;
		var tipsWidth = msg.length * 15 + 40;//tipsDiv.width();
		tipsWidth = tipsWidth > 400 ? 400 : tipsWidth;
		var left = (width / 2) - (tipsWidth / 2);
		
		tipsDiv.css({
			'top' : '1px',
			left: left + 'px',
			'position' : 'absolute',
			'padding' : '10px 30px 10px 30px',
			'border': "1px #ff0000 solid",
			'background-color': '#FFFfFF',
			'font-size' : '14px',
			'margin' : '0 auto',
			'text-align': 'center',
			'width' : tipsWidth + "px",
			'z-index': 9999999
		}).show();
		setTimeout(function() {
			tipsDiv.fadeOut(200, function() {
				tipsDiv.remove();
			});
		}, 4000);
	},
	/**
	 * iframe弹出窗体
	**/
	window: function(url, json, noRemove) {
		var jq = $;
		var iframeId = "iframe" + new Date().getTime();
		var time = new Date().getTime();
		if (url.indexOf("?") > -1) {
			url += "&t=" + time;
		} else {
			url += "?t=" + time;
		}
		var win = jq('<div class="easyui-window" id="open_window_' + time + '">' +
				'<iframe width="100%" height="100%" frameborder="0" name="' + iframeId + '" src="' + url + '"></iframe></div>');
		jq(window.document.body).append(win);
		var param = {
		    title: "",
		    width: 600,
		    height: 300,
		    shadow: true,
		    modal: true,
		    closed: true,
		    minimizable: false,
		    maximizable: false,
		    collapsible: false,
		    onClose: function() {
				if (!noRemove) {
		    		win.remove();
		    	}
		    }
		};
		if (top.o_pen_win == undefined) {
			top.o_pen_win = {};
			top.o_pen_win_dialog = {};
		}
		for (var key in json) {
			param[key] = json[key];
		}
		if (param["height"] && (param["height"] + "").indexOf("%") > 1) {
			param["height"] = top.window.document.body.clientHeight * parseInt(param["height"]) / 100;
		}
		if (param["width"] && (param["width"] + "").indexOf("%") > 1) {
			param["width"] = top.window.document.body.clientWidth * parseInt(param["width"]) / 100;
		}
		var dialog = win.window(param);
		dialog.close = function() {
			dialog.window('close');
		}
		dialog.open = function() {
			dialog.window('open');
		}
		dialog.removeWindow = function() {
		    win.remove();
		}
		dialog.open();
		
		top.o_pen_win_dialog[time] = dialog;
		top.o_pen_win[time] = window;
		return dialog;
	},
	loadding: function(bool) {
		var jq = bool ? jQuery : (top.jQuery ? top.jQuery : jQuery);
		var load = null;
		try {
			load = jq.messager.progress();
			load.close = function() {
	            jq.messager.progress('close');
			}
		} catch (e) {
			load = {close: function() {}};
		}
		return load;
	},
	getOpenWindow: function() {
		var win = null;
		if (this.request("t")) {
			win = top.o_pen_win[this.request("t").replace(/\#/g, "")];
		}
		if (!win) {
			win = window;
		}
		return win;
	},
	closeWindow: function() {
		if (this.request("t")) {
			var win = top.o_pen_win_dialog[this.request("t").replace(/\#/g, "")];
			if (win) {
				win.close();
			}
		}
	},
	/**
	 * 自动填充页面信息
	*/
	initPageEditData: function(url, params, addFix) {
		$.post(url, params, function(data) {
			if (data.success == false) {
				alert(data.msg);
			} else {
				if (data.data) {
					data = data.data;
				}
				for (var key in data) {
					$("#" + (addFix ? addFix + "." : "") + key).val(data[key]);
				}
			}
		}, "json");
	},
	getForm: function(id, add_att) {
		return this.dis_form_data.getForm(id, add_att);
	},
	dis_form_data: {
		getForm: function(id, add_att) {
			return this.init(id, add_att);
		},
		init: function(id, add_att) {
			function zfFome() {}
			zfFome.prototype = {
				val_bool: true,
				items: {},
				getItems: function() {
					return this.items;
				},
				isVal: function() {
					return this.val_bool;
				},
				parseData: function() {
					var inputs = $("#" + this.id + " input");
					var checkboxes = {};
					for (var i = 0;i < inputs.length;i ++) {
						if (this.verification(inputs[i])) {
							return;
						}
						var typeName = $(inputs[i]).attr("type");
						if (typeName && typeName.toUpperCase() == "CHECKBOX") {
							var name = $(inputs[i]).attr("name");
							if (checkboxes[name] == undefined) {
								var boxes = document.getElementsByName(name);
								for (var b = 0;b < boxes.length;b ++) {
									if (boxes[b].checked) {
										checkboxes[name] = boxes[b].value;
										this.addValue(name, boxes[b].value);
										break;
									}
								}
							}
							continue;
						} else if (typeName && typeName.toUpperCase() == "RADIO") {
							var name = $(inputs[i]).attr("name");
							if (checkboxes[name] == undefined) {
								var values = [];
								var boxes = document.getElementsByName(name);
								for (var b = 0;b < boxes.length;b ++) {
									if (boxes[b].checked) {
										values.push(boxes[b].value);
									}
								}
								checkboxes[name] = values.join(",");
								this.addValue(name, checkboxes[name]);
							}
							continue;
						} else {
							this.addValue(inputs[i].id, inputs[i].value);
						}
					}
					var selects = $("#" + this.id + " select");
					for (var i = 0;i < selects.length;i ++) {
						if (this.verification(selects[i])) {
							return;
						}
						this.addValue(selects[i].id, selects[i].value);
					}
					var textareas = $("#" + this.id + " textarea");
					for (var i = 0;i < textareas.length;i ++) {
						if (this.verification(textareas[i])) {
							return;
						}
						this.addValue(textareas[i].id, textareas[i].value);
					}
				},
				addValue: function(id, value) {
					if (this.add_att) {
						this.items[this.add_att + "." + id] = value;
					} else {
						this.items[id] = value;
					}
				},
				verification: function(obj) {
					var rule = $(obj).attr("rule");
					var bool = false;
					if (rule) {
						var rules = rule.replace(/\ /g, "").split(";");
						for (var j = 0;j < rules.length;j ++) {
							if (this.isNotNull(obj, rules[j])) {
								bool = true;
								break;
							}
							if (this.length(obj, rules[j])) {
								bool = true;
								break;
							}
							if (this.maxLength(obj, rules[j])) {
								bool = true;
								break;
							}
							if (this.isNumber(obj, rules[j])) {
								bool = true;
								break;
							}
						}
						if (bool) {
							$(obj).addClass("is_not_rule");
							window.setTimeout(function() {
								$(obj).removeClass("is_not_rule");
							}, 5000);
						}
					}
					return false;
				},
				isNotNull: function(obj, rule) {
					var strs = rule.split(":");
					//验证是否为空
					if (strs[0].toUpperCase() == "ISNOTNULL" && $(obj).val() == "") {
						this.val_bool = false;
						$(obj).attr("title", "不能为空！");
						return true;
					}
				},
				length: function(obj, rule) {
					var strs = rule.split(":");
					//验证长度是否对的
					if (strs[0].toUpperCase() == "LENGTH" && $(obj).val().length != strs[1] * 1) {
						this.val_bool = false;
						$(obj).attr("title", "长度只能等于" + strs[1] + "！");
						return true;
					}
				},
				maxLength: function(obj, rule) {
					var strs = rule.split(":");
					//验证长度是否对的
					if (strs[0].toUpperCase() == "MAXLENGTH" && $(obj).val().length > strs[1] * 1) {
						this.val_bool = false;
						$(obj).attr("title", "长度不能大于" + strs[1] + "！");
						return true;
					}
				},
				isNumber: function(obj, rule) {
					var strs = rule.split(":");
					var value = $(obj).val();
					//验证长度是否对的
					if (value != "" && strs[0].toUpperCase() == "ISNUMBER" && isNaN($(obj).val() * 1)) {
						this.val_bool = false;
						$(obj).attr("title", "只能为数字类型！");
						return true;
					}
				}
			}
			var form = new zfFome();
			form.id = id;
			form.add_att = add_att;
			form.parseData();
			return form;
		}
	},
	request: function (paras) {
		if (this.url_paraObj == undefined) {
			var search = window.location.search;
			if (search) {
				search = search.substring(1);
			}
			var paraString = search.split("&");
			this.url_paraObj = {}
			for (var i = 0; i < paraString.length; i++) {
				var index = paraString[i].indexOf("=");
				try {
					this.url_paraObj[paraString[i].substring(0, index)] = decodeURI(paraString[i].substring(index + 1));
				} catch (e) {
					this.url_paraObj[paraString[i].substring(0, index)] = paraString[i].substring(index + 1);
				}
			}
		}
		return paras ? this.url_paraObj[paras] : this.url_paraObj;
	},
	downFile: function(filePath, fileName) {
		if (this.downFileIframe == undefined) {
			this.downFileIframe = $('<iframe frameborder="0" style="display: none;"></iframe>');
			$(window.document.body).append(this.downFileIframe);
		}
		this.downFileIframe.attr("src", host + "downFileServlet?filePath=" + encodeURIComponent(filePath) + 
				"&fileName=" + encodeURIComponent(fileName));
	},
	selectMenu: function(array, obj) {
		if (this.SelectMenu_prototype == undefined) {
			this.SelectMenu_prototype = {
				minWidth: 130,//最小宽度
				maxWidth: 170,//最大宽度
				init: function(array, obj) {
					if (this.panel == undefined) {
						this.panel = $("<div class='selectMenu_panel'></div>");
						$(document.body).append(this.panel);
						this.addEvent();
					}
					this.panel.css("display", "");
					var maxWidth = this.minWidth,w;
					for (var i = 0;i < array.length; i += 1) {
						if (!array[i].hr) {
							w = Math.ceil(this.getStrLength(array[i].text) * 7.5);
							if (w > maxWidth)
								maxWidth = w;
						}
					}
					if (maxWidth > this.maxWidth)
						maxWidth = this.maxWidth;
					var html = [];
					var json;
					html.push("<div class='list'>");
					for (var i = 0;i < array.length; i += 1) {
						json = array[i];
						if (json.hr) {
							if (i != 0)
								html.push("<div class='items' style='width: " + maxWidth + 
										"px;line-height: 1px;height: 1px;border-top: 1px #AAAAAA solid;'></div>");
							continue;
						}
						html.push("<div class='items'");
						if (json.style) {
							html.push(" style='" + json.style + "'");
						}
						html.push(">");
						html.push("<a href='javascript: void();' onclick='");
						html.push(json.fun);
						html.push(";baseJs.SelectMenu_prototype.hidden();return false;'");
						html.push(" style='width: " + maxWidth + "px;'>" + json.text + "</a></div>");
					}
					html.push("</div>");
					this.panel.html(html.join(""));
					var top = 0,left = 0;
					if (obj) {
						var o = obj;
						var height = o.childNodes.length > 0 && o.childNodes[0].offsetHeight > o.offsetHeight
										? o.childNodes[0].offsetHeight : o.offsetHeight;
						top = height + 2;
						while (o && o.tagName != "HTML") {
							top += o.offsetTop - o.scrollTop;
							left += o.offsetLeft - o.scrollLeft;//减去滚动条滚动量
							o = o.offsetParent;
						}
					} else {
						top = event.clientY + 2;
						left = event.clientX;
					}
					//判断显示后是否超过窗口的边界
					if (document.body.clientHeight > 400 && document.body.clientHeight < this.panel[0].clientHeight + top)
						top = top - this.panel[0].offsetHeight - height - 2;
					if (document.body.clientWidth < this.panel[0].clientWidth + left)
						left = left - (this.panel[0].offsetWidth);
					this.panel.css("top", top);
					this.panel.css("left", left);
				},
				addEvent: function() {
					$(document).bind("mousedown", function(e) {
						try {
							if (!baseJs.SelectMenu_prototype.isHidden()) {
								var o = e.srcElement || e.target;
								while (o && o.tagName != "BODY") {
									if (o == baseJs.SelectMenu_prototype.panel[0])
										return;
									o = o.offsetParent;
								}
								baseJs.SelectMenu_prototype.hidden();
							}
						} catch (e) {
						}
					});
				},
				isHidden: function() {
					if (this.panel && this.panel.css("display") != "none")
						return false;
					return true;
				},
				hidden: function() {
					this.panel.css("display", "none");
				},
				getStrLength: function(value) {
					 return value.replace(/[<>]/g, "d").replace(/[\u4e00-\u9fa5]/g, "dd").length;
				}
			};
		}
		this.SelectMenu_prototype.init(array, obj);
	},
	confirm: function(title, msg, fun) {
		$.messager.defaults = {ok: "确定", cancel: "取消"}; 
		$.messager.confirm(title, msg, function(r){
			if (r) {
				fun(r);
			}
		});
	},
	alert: function(msg) {
		var parentWin = baseJs.getOpenWindow();
		if (parentWin) {
			parentWin.alert(msg);
		}
	}
};
function getUrlParam(name) {
	return baseJs.request(name);
}
function openNewWindow(url, height, widht, title, topInside) {
	return baseJs.window(url, {width: widht, height: height, title: title}, topInside);
}
function closeWin() {
	baseJs.closeWindow();
}
function submitData(url, params, rollbackFun, msg, method) {
	baseJs.confirm("确认", msg, function(r) {
		if (r) {
			$.ajax({
				url: url,
				type: method ? method : 'GET',
				async: true,
				data: params,
				dataType: 'json',
				success: function(data) {
					rollbackFun(data);
				},
		        error: function (XMLHttpRequest, textStatus, errorThrown) {
					rollbackFun(errorThrown);
		        },
		        complete: function(XMLHttpRequest, textStatus){
		        }
			});
		}
	});
}
//生成toolbar
$(function() {
	$(".easyui-toolbar a").each(function(i, a) {
		$(a).linkbutton({plain: true});
	});
});
var windAlert = alert;
var alert = function(msg) {
	if (top.layer) {
		top.layer.msg(msg);
	} else {
		windAlert(msg);
	}
}
function alertObject(obj) {
	var array = [];
	for (var key in obj) {
		array.push(key + ": " + obj[key]);
	}
	alert(array.join(", "));
}
Date.prototype.toString = function() {
	return this.getFullYear() + "-" + 
		(this.getMonth() > 8 ? this.getMonth() + 1 : "0" + (this.getMonth() + 1)) + "-" + 
		(this.getDate() > 9 ? this.getDate() : "0" + (this.getDate()));
}
try {
	JSON.stringify({});
} catch (e) {
	JSON = {};
}
JSON.stringify = function(value){
	if (value == null || typeof value == 'number' || typeof value == 'boolean') {
		return value;
	} else if(Object.prototype.toString.apply(value) === '[object Array]') {
		var data = [];
		for (var i = 0,len = value.length; i < len; i++) {
			data.push(this.stringify(value[i]));
		}
		return "[" + data.join(",") + "]";
	} else if (typeof value == 'object') {
		var data = [];
		for (var key in value) {
			if (typeof value[key] != 'function' && value[key] !== null) {
				var v = this.stringify(value[key]);
				if (typeof v == "string") {
					data.push('"' + key + '":"' + v + '"');
				} else {
					data.push('"' + key + '":' + v);
				}
			}
		}
		return "{" + data.join(",") + "}";
	}
	return value;
};
JSON.toString = JSON.stringify;